package org.itboys.mobile.controller.mobile;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.SundayAddressDto;
import org.itboys.mobile.dto.TianJieFinanceAccountDto;
import org.itboys.mobile.entity.mongo.SundayAddress;
import org.itboys.mobile.service.mongo.mobile.SundayMobileAddressService;
import org.itboys.mobile.service.mongo.mobile.TianJieMobileAccountService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：天街_金融_全球购_收货地址_controller_mobile
 */
@Controller
@RequestMapping("/tianjie/mobile/finance/globe/address")
public class TianJieMobileAddressController extends BaseController{
    @Autowired
    private SundayMobileAddressService addressService;

    @RequestMapping(value="/getAddress",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【全球购-收货地址】获取收货地址(不分页,默认地址会排在前面)")
    public void getAddress(HttpServletRequest request,
                           HttpServletResponse response,
                           @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                           @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();

            List<SundayAddressDto> accounts=addressService.getAddress(memberId);
            ResponseMessage.success(accounts);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/mobile/finance/globe/address/getAddress----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    @RequestMapping(value="/save",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【全球购-收货地址】新增/或修改收货地址。")
    public void save(HttpServletRequest request,
                          HttpServletResponse response,
                     @ApiParam(required=true, name="id", value="地址id。新增时传入0")@RequestParam(value = "id", required = false)Long id,
                      @ApiParam(required=true, name="receiveName", value="收货人姓名")@RequestParam(value = "receiveName", required = true)String receiveName,
                     @ApiParam(required=true, name="receiveMobile", value="手机号码")@RequestParam(value = "receiveMobile", required = true)String receiveMobile,
                     @ApiParam(required=false, name="provinceId", value="省ID。")@RequestParam(value = "provinceId", required = false)Long provinceId,
                     @ApiParam(required=false, name="provinceName", value="省名称。")@RequestParam(value = "provinceName", required = false)String provinceName,
                     @ApiParam(required=false, name="cityId", value="市ID。")@RequestParam(value = "cityId", required = false)Long cityId,
                     @ApiParam(required=false, name="cityName", value="市名称。")@RequestParam(value = "cityName", required = false)String cityName,
                     @ApiParam(required=false, name="districtId", value="区县ID。")@RequestParam(value = "districtId", required = false)Long districtId,
                     @ApiParam(required=false, name="districtName", value="区县名称。")@RequestParam(value = "districtName", required = false)String districtName,
                     @ApiParam(required=true, name="receiveAddress", value="详细地址")@RequestParam(value = "receiveAddress", required = true)String receiveAddress,
                     @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                     @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            addressService.saveAddress(id,memberId,receiveName,receiveMobile,provinceId,provinceName,cityId,cityName,districtId,districtName,receiveAddress);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/mobile/finance/globe/address/save----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/delete",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【全球购-收货地址】删除收货地址")
    public void delete(HttpServletRequest request,
                     HttpServletResponse response,
                     @ApiParam(required=true, name="addressId", value="地址ID")@RequestParam(value = "addressId", required = true) Long addressId,
                     @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = false) String nonce,
                     @ApiParam(value = "签名")@RequestParam(value = "sign", required = false) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            addressService.delete(addressId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/mobile/finance/globe/address/delete----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/setDefault",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【全球购-收货地址】设置默认地址")
    public void setDefault(HttpServletRequest request,
                       HttpServletResponse response,
                       @ApiParam(required=true, name="addressId", value="地址ID")@RequestParam(value = "addressId", required = true) Long addressId,
                       @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = false) String nonce,
                       @ApiParam(value = "签名")@RequestParam(value = "sign", required = false) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            addressService.setDefaultAddress(addressId,memberId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/mobile/finance/globe/address/setDefault----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
}
